package qc.module.ehs.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.module.ehs.dto.warehouseinventory.WareHouseInventoryAddDto;
import qc.module.ehs.dto.warehouseinventory.WareHouseInventoryDto;
import qc.module.ehs.dto.warehouseinventory.WareHouseInventoryQueryConditionDto;
import qc.module.ehs.service.EhsWarehouseInventoryService;
import qc.module.platform.dto.uac.UacUserInfoDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * WareHouseInventoryController
 * 仓库出入库
 *
 * @author QuCheng Tech
 * @create 2024/11/11
 */
@RestController
@RequestMapping("/warehourinventory")
public class WareHouseInventoryController {
    
    @Autowired
    private EhsWarehouseInventoryService service;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 查询仓库出入库记录
     * 
     * @param condition 查询条件DTO
     * @return List<WareHouseInventoryDto>
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @RequestMapping(value = "/query",method = {RequestMethod.GET,RequestMethod.POST})
    public List<WareHouseInventoryDto> query(@RequestBody WareHouseInventoryQueryConditionDto condition) throws QCPromptException {
        return service.query(condition);
    }

    /**
     * 获取指定仓库出入库记录
     * 
     * @param id 出入库记录ID
     * @return WareHouseInventoryDto
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @GetMapping("/get")
    public WareHouseInventoryDto getByParam(@RequestParam String id){
        return service.get(id);
    }

    /**
     * 获取指定仓库出入库记录
     *
     * @param id 出入库记录ID
     * @return WareHouseInventoryDto
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @GetMapping("/get/{id}")
    public WareHouseInventoryDto getByPath(@PathVariable String id){
        return service.get(id);
    }

    /**
     * 仓库出入库填单
     * 每个出入库记录分为填单和复核2步，由不同人员进行操作；
     * 
     * @param addDto
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    public String add(@RequestBody WareHouseInventoryAddDto addDto) throws QCPromptException {
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));
        //根据token获取当前用户信息
        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);

        return service.add(addDto, userInfo);
    }

    /**
     * 仓库出入库复核
     * 每个出入库记录分为填单和复核2步，由不同人员进行操作；
     * 
     * @param id 出入库记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @RequestMapping(value = "/check",method = {RequestMethod.POST})
    public String checkByParam(@RequestParam String id) throws QCPromptException {
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));
        //根据token获取当前用户信息
        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);

        return service.check(id, userInfo);
    }

    /**
     * 仓库出入库复核
     * 每个出入库记录分为填单和复核2步，由不同人员进行操作；
     *
     * @param id 出入库记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @RequestMapping(value = "/check/{id}",method = {RequestMethod.POST})
    public String checkByPath(@PathVariable String id) throws QCPromptException {
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));
        //根据token获取当前用户信息
        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);

        return service.check(id, userInfo);
    }

    /**
     * 删除
     *
     * @param id 出入库记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @DeleteMapping("/delete")
    public String deleteByParam(@RequestParam String id){
        return service.delete(id);
    }

    /**
     * 删除
     *
     * @param id 仓库ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/11/12
     */
    @DeleteMapping("/delete/{id}")
    public String deleteByPath(@PathVariable String id){
        return service.delete(id);
    }
}
